package fr.inserm.umr915.vcfannotator;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * AnnotatorList
 */
public class AnnotatorList
	{
	private static final Log LOG = LogFactory.getLog("vcf.annotator"); 
	private String name;
	private String description;
	private List<Annotator> annotators=new ArrayList<Annotator>();
	
	public String getName()
		{
		return this.name;
		}		
	
	public void setName(String name)
		{
		this.name = name;
		}
	
	public String getDescription()	
		{
		if(description!=null)  return description;
		StringBuilder b=new StringBuilder(getName());
		b.append("\n");
		b.append(String.valueOf(getAnnotators().size()));
		b.append(" annotators\n");
		for(Annotator an: getAnnotators())
			{
			b.append("  * ");
			b.append(an.getName());
			b.append(" : ");
			b.append(an.getDescription());
			b.append("\n");
			}
		return b.toString();
		}
	
	public void setDescription(String description)
		{
		this.description = description;
		}
	
	public List<Annotator> getAnnotators()
		{
		return this.annotators;
		}
	
	public void setAnnotators(List<Annotator> annotators)
		{
		this.annotators = annotators;
		}
	
	public void annotate(BaseDatabase database) throws AnnotationException
		{
		LOG.info("begin annotate annotators:"+getAnnotators().size());
		for(Annotator an:getAnnotators())
			{
			LOG.info("Running annotator:"+an.getName());
			try
				{
				an.init(database);
				an.annotate();
				}
			catch(AnnotationException err)
				{
				LOG.error(an, err);
				throw err;
				}
			finally
				{
				an.cleanup();
				}
			}
		LOG.info("end annotate annotators:"+getAnnotators().size());
		}
	
	@Override
	public String toString()
		{
		return getName();
		}
	}
